//
//  XFYPlistTool.m
//  SDKSample
//
//  Created by 小肥羊 on 15/10/21.
//  Copyright © 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYPlistTool.h"

@implementation XFYPlistTool

#pragma mark - 方法
/*! @brief 创建Plist文件 */
+ (void)createArrayPlistWithName:(NSString *)plistName
{
    if ([plistName isEqualToString:@""]) {
        NSLog(@"文件名不能为空");
        return;
    }
    NSString *plistPath = [self nameToPath:plistName];
    //判断是否以创建文件
    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        //此处可以自己写显示plist文件内容
        NSLog(@"文件已存在");
    }
    else
    {
        //如果没有plist文件就自动创建
        NSMutableArray *messageList = [[NSMutableArray alloc] init];
        [messageList writeToFile:plistPath atomically:YES];
        NSLog(@"已创建:%@",plistPath);
    }
}

/*! @brief 文件Array */
+ (NSMutableArray *)PlistWithName:(NSString *)plistName
{
    NSString *plistPath = [self nameToPath:plistName];
    NSMutableArray *messageList = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath] mutableCopy];
    return messageList;
}

/*! @brief 添加信息 */
+ (NSMutableArray *)addDictInArrayPlist:(NSString *)plistName Dict:(NSMutableDictionary *)dict
{
    NSString *plistPath = [self nameToPath:plistName];
    NSMutableArray *messageList = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath] mutableCopy];
    if (messageList) {
        [messageList addObject:dict];
        [messageList writeToFile:plistPath atomically:YES];
        NSLog(@"添加信息");
    }
    return messageList;
}

/*! @brief 添加信息 */
+ (NSMutableArray *)deletDictInArrayPlist:(NSString *)plistName AtIndex:(NSInteger)index
{
    NSString *plistPath = [self nameToPath:plistName];
    NSMutableArray *messageList = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath] mutableCopy];
    if (messageList) {
        [messageList removeObjectAtIndex:index];
        [messageList writeToFile:plistPath atomically:YES];
        NSLog(@"删除信息");
    }
    return messageList;
}

/*! @brief 修改信息 */
+ (NSMutableArray *)modifyDictInArrayPlist:(NSString *)plistName AtIndex:(NSInteger)index Dict:(NSMutableDictionary *)dict
{
    NSString *plistPath = [self nameToPath:plistName];
    NSMutableArray *messageList = [[[NSMutableArray alloc] initWithContentsOfFile:plistPath] mutableCopy];
    if (messageList) {
        [messageList setObject:dict atIndexedSubscript:index];
        [messageList writeToFile:plistPath atomically:YES];
        NSLog(@"修改信息");
    }
    return messageList;
}

/*! @brief 转化路径 */
+ (NSString *)nameToPath:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",name]];
    return plistPath;
}

@end
